package yxy.game.pm2.active.command.flop;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actfloprandom;
import yxy.cherry.data.bean.actfloptarget;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_翻牌;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.flop.FlopCard;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Map;

/**
 * 翻牌 - 抽奖信息
 *
 * @author Dell
 */
@MessageType(proto = Command.ActFlopCardLuckDrawInfo, classtype = ActiveMessage.ActFlopCardLuckDrawInfoRequest.class)
public class CMD_Active_翻牌_抽奖_Info extends Command<ActiveMessage.ActFlopCardLuckDrawInfoRequest> {

    private static final Logger logger = Logger.create(CMD_Active_翻牌_抽奖_Info.class);

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActFlopCardLuckDrawInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        ActiveMessage.ActFlopCardLuckDrawInfoResponse.Builder response = ActiveMessage.ActFlopCardLuckDrawInfoResponse.newBuilder();

        ActiveModule_翻牌 am = ActiveModule_翻牌.get(playercode, false);
        if (am == null) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        if (!am.running()) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        response.setTurnnum(am.getFcturn());
        response.setGrandid(am.getSelectid());

        Currency item = Item.get(playercode, CurrencyType.CT_物品, 1038);    //翻牌 - 翻牌道具
        response.setItemnum(item.getNumber());

        //翻牌
        Map<String, actfloprandom> afrmap = ActDataCenter.getAllData(actfloprandom.class, playercode);
        for (FlopCard fc : am.getFcmap().values()) {
            ActiveMessage.FlopCardInfo.Builder build = ActiveMessage.FlopCardInfo.newBuilder();
            build.setPosition(fc.getPosition());

            boolean flop = fc.isFlop();
            build.setFlop(flop);
            if (flop) {
                if (fc.isGrand()) {
                    actfloptarget data = ActDataCenter.getData(am.getSelectid(), actfloptarget.class, playercode);
                    if (data != null) {
                        Currency cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
                        PackageMessage.Item curitem = new CurrencyHandle().packItem(cur);
                        build.setGrand(true);
                        build.setCur(curitem);
                    }
                } else {
                    actfloprandom data = afrmap.get(String.valueOf(fc.getId()));
                    Currency cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
                    PackageMessage.Item curitem = new CurrencyHandle().packItem(cur);
                    build.setCur(curitem);
                }
            }

            response.addFcs(build.build());
        }

        //大奖
        Map<String, actfloptarget> aftmap = ActDataCenter.getAllData(actfloptarget.class, playercode);
        for (actfloptarget aft : aftmap.values()) {
            ActiveMessage.FlopCardGrand.Builder build = ActiveMessage.FlopCardGrand.newBuilder();
            build.setId(aft.getId());
            build.setCondition(aft.getTurn());
            Currency cur = Currency.create(aft.getRewardtype(), aft.getRewardid(), aft.getRewardnum());
            PackageMessage.Item curitem = new CurrencyHandle().packItem(cur);
            build.setCur(curitem);

            int residueNum = -1;
            if (aft.getChoosetimes() != -1) {
                if (am.getSelectTimes() != null && am.getSelectTimes().containsKey(aft.getId())) {
                    residueNum = aft.getChoosetimes() - am.getSelectTimes().get(aft.getId());
                } else {
                    residueNum = aft.getChoosetimes();
                }
            }
            build.setResidueNum(residueNum);
            response.addFcgs(build.build());
        }

        response.setStart(am.getTimeline().getStartTime().getMillis());
        response.setEnd(am.getTimeline().getEndTime().getMillis());
        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, ActiveMessage.ActFlopCardLuckDrawInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
